Introduction To Socket Programming In Java

Introduction

 
In this article, we discuss socket programming in Java.
 
Before explaining sockets we need to learn some concepts of networking.
 

Networking

 
Networking is a concept of connecting two or more computing devices together so that we can share resources. The term network programming refers to writing programs that execute across multiple devices (computers), in which the devices are all connected to each other using a network.
 

Advantages

  • Provides flexible access to files and data over a network.
  • Sharing resources.
  • Security.
  • Speed.
  • Centralized software management.
  • Provide security like sending sensitive (password protected) files and programs on a network.

Network Terminology

 
The following are networking terms:
  • MAC Address
  • IP address
  • Protocol
  • Port no.
  • Connection-oriented and Connectionless protocol
  • Socket

IP Address

 
IP Address is a unique number assigned to a node of a network, for example, 192.168.0.152. It is composed of octets that range from 0 to 255.
 

Protocol

 
Protocol is a set of defined rules for communication between client and server. For example:
  • SMTP
  • TCP
  • FTP
  • Telnet
  • POP etc.
Now the discussion.
 

What Socket Programming in Java?

 
Socket Programming is used for communication between machines using a Transfer Control Protocol (TCP). It can be connectionless or connection-oriented. ServerSocket and Socket classes are used for connection-oriented socket programming.  After creating a connection, the server develops a socket object on its end of the connection. The server and client now starts communicating by writing to and reading from the socket.
 
The client needs to learn two basic peices of information, which are:
  1. Port number
  2. IP address of server

Socket class in Java

 
A socket is simply an endpoint for communications between the machines. The Socket class can be used to create a socket.
The following figure shows a communication structure between two machines, in this figure, the communication is done using a socket over the internet.
 
Socket.jpg 
Some commonly used public methods of the Socket class are:
  1. synchronized void close()
  2. Socket (String host, int port) thows UnknownHostException, IOException
  3. Socket()
  4. OutputStream getOutputStream()
  5. InputStream getInputStream()

ServerSocket class in Java

 
This class can be used to create a server socket. This object establishes the communication with the clients.
 
The java.net.ServerSocket class is used by server applications to obtain a port and listen for client requests
 
Some commonly used public methods of the ServerSocket class are:
  1. OutputStream getOutputStream()
  2. synchronized void close()
  3. Socket accept()
  4. InputStream getInputStream()
Example
 
In this example, we create two classes OurServer.java and OurClient.java. The first class (OurServer.java) is for the server and the second class (OurClient.java) is for the client. When we run our program it shows the communication between both the client and the server, when we run the client program a message is generated on the server side that indicates the connection between them.
 
OurServer.java
  1. import java.net.*;  
  2. import java.io.*;  
  3. public class OurServer  
  4. {  
  5.     public static void main(String args[])  
  6.     {  
  7.         try  
  8.         {  
  9.             ServerSocket serskt = new ServerSocket(9999);  
  10.             Socket skt = serskt.accept(); //establishes connection  
  11.             DataInputStream dinptstr = new DataInputStream(skt.getInputStream());  
  12.             String st = (String) dinptstr.readUTF();  
  13.             System.out.println("message= " + st);  
  14.             serskt.close();  
  15.         } catch (Exception ex)  
  16.         {  
  17.             System.out.println(ex);  
  18.         }  
  19.     }  
  20. }  
OurClient.java
  1. import java.net.*;  
  2. import java.io.*;  
  3. public class OurClient  
  4. {  
  5.     public static void main(String args[])  
  6.     {  
  7.         try  
  8.         {  
  9.             Socket skt = new Socket("localhost"9999);  
  10.             DataOutputStream doutstr = new DataOutputStream(skt.getOutputStream());  
  11.             doutstr.writeUTF("Connect To Server");  
  12.             doutstr.flush();  
  13.             doutstr.close();  
  14.             skt.close();  
  15.         } catch (Exception ex)  
  16.         {  
  17.             System.out.println(ex);  
  18.         }  
  19.     }  
  20. }  
Output
 
To get the proper output, in other words, output that shows a communication, we need to open two command shells. To start the compilation, first, we must compile and run the OurServer.java file then we must wait until the OurClient.java file is not run on the second command shell. 
 
Fig-1.jpg
In the previous figure, we saw a message is generated showing ("Connect To Server"). This message is generated after compiling and running the OurClient.java file. This shows communication between both the client and the server.
 
Fig-2.jpg 


Similar Articles